package www.dianbaozu.com.ui.server

import android.view.View
import androidx.databinding.ObservableField
import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import okhttp3.MultipartBody
import www.dianbaozu.com.R
import www.wen.com.common.lib.base.module.BaseApiViewModel
import www.wen.com.common.lib.data.entities.LoadingDialog
import www.wen.com.common.lib.data.entities.LoadingDialogStateType
import www.wen.com.common.lib.data.entities.RequestBodyParameter
import www.wen.com.common.lib.data.entities.UiStateCustom
import www.wen.com.common.lib.data.repository.AppDataManager
import www.wen.com.common.lib.extension.getString


/**
 * @author WEN
 * @Description:创建新的服务器
 * @date 2020//25 17:48
 */
class CreateServerViewModel @ViewModelInject constructor(appDataManager: AppDataManager) : BaseApiViewModel(appDataManager) {

    var mAvatar = ""

    val mServerName = ObservableField("")

    val mEnableCreate = ObservableField(true)

    val mIsShowErrorTip = ObservableField(View.INVISIBLE)

    val mErrorTipText = ObservableField("")


    /**
     * 创建新的服务器
     */
    fun requestCreateServer(
            partMap: Map<String, okhttp3.RequestBody>,
            serverName: String,
            vararg files: MultipartBody.Part,
            action: (() -> Unit)? = null,
    ) {
        viewModelScope.launch {
            appDataManager.uploadOssFileFlow(partMap, *files)
                    .flatMapConcat {
                        appDataManager.createServerFlow(RequestBodyParameter(
                                serverLogo = it.data.content[0],
                                serverName = serverName,
                        ))
                    }
                    .onStart {
                        loading.value = UiStateCustom(
                                stateData = LoadingDialog(
                                        getString(R.string.being_created),
                                        LoadingDialogStateType.LOADING
                                )
                        )
                    }
                    .onCompletion { cause ->
                        if (cause == null) {
                            loading.value = UiStateCustom(
                                    stateData = LoadingDialog(
                                            tip = getString(R.string.created_successfully),
                                            loadingDialogStateType = LoadingDialogStateType.SUCCESS,
                                            isShowDialogTip = false,
                                            isShowToastTip = true
                                    )
                            )
                        }
                    }
                    .catch { cause ->
                        loading.value = UiStateCustom(
                                stateData = LoadingDialog(
                                        tip = cause.message ?: "网络异常",
                                        loadingDialogStateType = LoadingDialogStateType.ERROR,
                                        isShowDialogTip = false,
                                        isShowToastTip = true
                                )
                        )
                        mIsShowErrorTip.set(View.VISIBLE)
                        mErrorTipText.set(cause.message)
                    }
                    .collect {

                        action?.invoke()
                    }
        }
    }

    fun onServerNameChanged(textChange: CharSequence) {
//        mEnableCreate.set(mServerName.get()!!.isNotBlank() && mAvatar.isNotBlank())
    }

    fun checkCanCreate(){
//        mEnableCreate.set(mServerName.get()!!.isNotBlank() && mAvatar.isNotBlank())
    }

}


